<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
		<button type="primary" @click="getUserProfile">一键登录</button>
		<text>登录后尽享更多权益</text>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-login",
		computed: {
			...mapState('user', ['redirectInfo'])
		},
		data() {
			return {

			};
		},
		methods: {
			...mapMutations('user', ['updateUserInfo', 'updateToken', 'upadateRedirectInfo']),
			getUserProfile() {
				uni.getUserProfile({
					desc: '获取信息',
					success: (e) => {
						this.updateUserInfo(e.userInfo)
						this.getToken(e)
					}
				})
			},
			async getToken(info) {
				const [err, res] = await uni.login().catch(err => err)
				if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败')
				const query = {
					code: res.code,
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature
				}
				const result = await uni.$http.post('/api/public/v1/users/wxlogin', query)
				this.updateToken(
					"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
				)
				if (this.redirectInfo && this.redirectInfo.opentype == 'switchTab') {
					uni[this.redirectInfo.opentype]({
						url: this.redirectInfo.from,
						complete: () => {
							this.upadateRedirectInfo(null)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 750rpx;
		background-color: #f8f8f8;

		button {
			width: 90%;
			background-color: #c00000;
			color: #fff;
			margin: 10px 0;
			border-radius: 100px;
		}

		text {}
	}
</style>